package test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class MergeTXT {
	public static void main(String[] args) {
		new MergeTXT().merge("D:/test/txt");
		/*File file = new File("D:/test/txt");
		File[] files = file.listFiles();
		for(int i = 0; i < files.length; i++){
			System.out.println("files[" + i + "]" + files[i]);
		}*/
	}
	
	public void merge(String txtPath){
		File file = new File(txtPath);
		File[] files = file.listFiles();
		File f = null;
		FileReader fr = null;
		BufferedReader br = null;
		FileWriter fw = null;
		BufferedWriter bw = null;
		//System.out.println(files[1].getPath());
		
		for(int i = 0; i < files.length; i++){
			f = new File(files[i].getPath());
			System.out.println(f);
			String line = null;
			try {
				fr = new FileReader(f);
				br = new BufferedReader(fr);
				while((line = br.readLine())!=null){
					File out = new File("D:/test/merge.txt");
					if(!out.exists())
						out.createNewFile();
					fw = new FileWriter(out,true);
					bw = new BufferedWriter(fw);
					bw.write(line);
					bw.newLine();
					bw.flush();
				}
				
				for(int j = 0;j < 5; j++){
					bw.newLine();
					bw.flush();
				}
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}catch (IOException e) {
				e.printStackTrace();
			}
			
		}
	}
}
